package pl.lsierant.j2me.bipacz.persistent;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class PersistentUtil {
    public Object deserialize(byte[] bytes) throws IOException {
        DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));

        int type = din.readInt();

        IPersistent result = PersistentObjectFactory.getInstance().create(type);
        result.initialize(din);

        return result;
    }

    public byte[] serialize(IPersistent persistent) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);

        persistent.serialize(dout);

        return bout.toByteArray();
    }

}
